package com.shijun.im.activity.interactor.impl;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.shijun.im.activity.interactor.LoginInteractor;
import com.shijun.im.application.MyAppLication;
import com.shijun.im.service.IMService;

public class LoginInteractorImpl implements LoginInteractor {

    @Override
    public void login(Context context,final String username, final String password, final LoginInteractor.OnLoginFinishedListener listener) {

        final IMService service = ((MyAppLication)context.getApplicationContext()).getIMService();
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(username==null||"".equals(username)){
                    listener.onError(LoginInteractor.LOGIN_USERNAMEERROR);
                }else if(password==null||"".equals(password)){
                    listener.onError(LoginInteractor.LOGIN_PASSWORDERROR);
                }else{
                    Boolean isRegistSuccess = service.login(username,password);
                    if(isRegistSuccess){
                        listener.onSuccess();
                    }else {
                        listener.onError(LoginInteractor.LOGIN_USERNAM_OR_PASSWORDEERROR);
                    }
                }
            }
        }).start();
    }
}
